﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ECare.BUS.Data;
using ECare.Database;

//........
namespace ECare.BUS
{
    public class SupplyInfoHelper
    {
        public static int CreateSupply(SupplyInfo supply)
        {
            using (var context = new Entities())
            {
                var newsupply = new supply();
                newsupply.resource_supply_id = supply.resource_supply_id;
                newsupply.resource_Id = supply.resource_id;

                newsupply.supply_code = supply.supply_code;
                newsupply.supply_date = supply.supply_date;
                newsupply.supply_number = supply.supply_number;
                newsupply.supply_unit = supply.supply_unit;
                newsupply.supply_kg = supply.supply_kg;
                
                newsupply.supply_create_time = DateTime.Now;
                newsupply.supply_create_by = ApplicationData.CurrentUserInfo.UserId;
                supply.supply_create_by = ApplicationData.CurrentUserInfo.UserId;

                newsupply.supply_update_by = ApplicationData.CurrentUserInfo.UserId;
                supply.supply_update_by = ApplicationData.CurrentUserInfo.UserId;


                newsupply.supply_update_time = DateTime.Now;

                newsupply.supply_isactive = true;

                newsupply.supply_remark = supply.supply_remark;

                newsupply.supply_produce_time = supply.supply_produce_time;
                newsupply.supply_invalid_time = supply.supply_invalid_time;

                newsupply.supply_sheet_no = supply.SheetNo;

                context.supplies.AddObject(newsupply);
                context.SaveChanges();

                return newsupply.supply_id;
            }
        }

        public static bool UpdateSupply(SupplyInfo supply)
        {
            using (var context = new Entities())
            {
                var newsupply = context.supplies.FirstOrDefault(a => a.supply_id == supply.supply_id);

                if (newsupply == null)
                    return false;

                newsupply.resource_supply_id = supply.resource_supply_id;
                newsupply.resource_Id = supply.resource_id;

                newsupply.supply_code = supply.supply_code;
                newsupply.supply_date = supply.supply_date;
                newsupply.supply_number = supply.supply_number;
                newsupply.supply_unit = supply.supply_unit;
                newsupply.supply_kg = supply.supply_kg;


                //newsupply.supply_create_time = DateTime.Now;
                //supply.supply_create_by=


                newsupply.supply_update_by = ApplicationData.CurrentUserInfo.UserId;
                newsupply.supply_update_time = DateTime.Now;

                newsupply.supply_isactive = true;

                newsupply.supply_remark = supply.supply_remark;

                newsupply.supply_produce_time = supply.supply_produce_time;
                newsupply.supply_invalid_time = supply.supply_invalid_time;

                newsupply.supply_sheet_no = supply.SheetNo;
                context.SaveChanges();

                return true;

            }
        }


        public static bool CheckSupply(SupplyInfo supply)
        {
            using (var context = new Entities())
            {
                var findsupply = context.supplies.FirstOrDefault(a => a.supply_id == supply.supply_id);
                if (findsupply == null)
                    return false;

                findsupply.supply_checkedstatus = (int)supply.supply_checkstatus;
                findsupply.supply_check_by = ApplicationData.CurrentUserInfo.UserId;//当前人 

                supply.supply_check_time = DateTime.Now;

                findsupply.supply_check_time = DateTime.Now;
                findsupply.supply_check_remark = supply.supply_check_remark;


                //如果不一致的话，就是修改有效期
                if (findsupply.supply_invalid_time != supply.supply_invalid_time)
                {
                    findsupply.supply_invalid_time = supply.supply_invalid_time;
                }

                context.SaveChanges();
                return true;
            }
        }


        public static List<SupplyInfo> GetAllSupplyInfo(string where="1=1")
        {
            using (var context = new Entities())
            {
                var res = new List<SupplyInfo>();

                //var supplies = context.supplies.Where(a => a.supply_isactive != false).OrderByDescending(a=>a.supply_create_time);

                var supplies = context.sp_supplyquery(where).ToList();

                foreach(var supply in supplies)
                {
                    var newsupply = new SupplyInfo();
                    newsupply.resource_id = supply.resource_Id.HasValue?supply.resource_Id.Value:0;
                    newsupply.resource_supply_id = supply.resource_supply_id.HasValue?supply.resource_supply_id.Value:0;


                    newsupply.supply_check_by = supply.supply_check_by.HasValue?supply.supply_check_by.Value:0;
                    newsupply.supply_check_time = supply.supply_check_time.HasValue?supply.supply_check_time.Value:DateTime.Now;
                    newsupply.supply_checkstatus = supply.supply_checkedstatus.HasValue ? (CheckStatus)supply.supply_checkedstatus.Value : CheckStatus.Pending;
                    newsupply.supply_check_remark = supply.supply_check_remark;


                    newsupply.supply_code = supply.supply_code;

                    newsupply.supply_create_by = supply.supply_create_by.HasValue?supply.supply_create_by.Value:0;
                    newsupply.supply_create_time = supply.supply_create_time.HasValue ? supply.supply_create_time.Value : DateTime.Now;
                    newsupply.supply_date = supply.supply_date.HasValue?supply.supply_date.Value:DateTime.MinValue;

                    newsupply.supply_id = supply.supply_id;
                    newsupply.supply_invalid_time = supply.supply_invalid_time.HasValue ? supply.supply_invalid_time.Value : DateTime.Now;
                    newsupply.supply_kg = supply.supply_kg.HasValue?supply.supply_kg.Value:0;
                    newsupply.supply_number = supply.supply_number.HasValue?supply.supply_number.Value:0;
                    newsupply.supply_remark = supply.supply_remark;
                    newsupply.supply_unit = supply.supply_unit;
                    newsupply.supply_update_by = supply.supply_update_by.HasValue?supply.supply_update_by.Value:0;
                    newsupply.supply_update_time = supply.supply_update_time.HasValue ? supply.supply_update_time.Value : DateTime.Now;


                    newsupply.HasStorage = supply.supply_hasStoraged.HasValue ? (supply.supply_hasStoraged.Value==1?true:false) : false;


                    newsupply.SheetNo = supply.supply_sheet_no;
                    res.Add(newsupply);

                }
                return res;
            }
        }



        public static DataTable GetPlanStatus()
        {
            var dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Name", typeof(String)));

        //     draft=0,
        //qualified=1,
        //rejected=2,
 
        //started=3,
        
        //pending=10,
        //done=11,
        //suspend=12,
        //working=15,
        //pause=16,

        //deleted=13
            var draftrow = dt.NewRow();
            draftrow[0] = 0;
            draftrow[1] = "draft";
            dt.Rows.Add(draftrow);


            var qualifiedrow = dt.NewRow();
            qualifiedrow[0] = 1;
            qualifiedrow[1] = "qualified";
            dt.Rows.Add(qualifiedrow);


            var rejecteddrow = dt.NewRow();
            rejecteddrow[0] = 2;
            rejecteddrow[1] = "rejected";
            dt.Rows.Add(rejecteddrow);

            var startedrow = dt.NewRow();
            startedrow[0] = 3;
            startedrow[1] = "started";
            dt.Rows.Add(startedrow);



            var pendingrow = dt.NewRow();
            pendingrow[0] = 10;
            pendingrow[1] = "pending";
            dt.Rows.Add(pendingrow);



            var donerow = dt.NewRow();
            donerow[0] = 11;
            donerow[1] = "done";
            dt.Rows.Add(donerow);


            var suspendrow = dt.NewRow();
            suspendrow[0] = 11;
            suspendrow[1] = "suspend";
            dt.Rows.Add(suspendrow);

            var workingrow = dt.NewRow();
            workingrow[0] = 12;
            workingrow[1] = "working";
            dt.Rows.Add(workingrow);


            var pauserow = dt.NewRow();
            pauserow[0] = 15;
            pauserow[1] = "pause";
            dt.Rows.Add(pauserow);



            var deletedrow = dt.NewRow();
            deletedrow[0] = 13;
            deletedrow[1] = "deleted";
            dt.Rows.Add(deletedrow);


            return dt;
        }


        public static DataTable GetSupplyStatus()
        {
            var dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Name", typeof(String)));

        //Pending=0,
        //Qualitified=1,
        //Rejected=2,
        //UnFinished=3
            var uncheckrow = dt.NewRow();
            uncheckrow[0] = 0;
            uncheckrow[1] = "Pending";
            dt.Rows.Add(uncheckrow);


            var Confirmedrow = dt.NewRow();
            Confirmedrow[0] = 1;
            Confirmedrow[1] = "Qualitified";
            dt.Rows.Add(Confirmedrow);


            var Faileddrow = dt.NewRow();
            Faileddrow[0] = 2;
            Faileddrow[1] = "Rejected";
            dt.Rows.Add(Faileddrow);

            var UnFinisheddrow = dt.NewRow();
            UnFinisheddrow[0] = 3;
            UnFinisheddrow[1] = "UnFinished";
            dt.Rows.Add(UnFinisheddrow);



            var expireddrow = dt.NewRow();
            expireddrow[0] = 4;
            expireddrow[1] = "Expired";
            dt.Rows.Add(expireddrow);

            //SpecialAcceptance
            var SpecialAcceptancerow = dt.NewRow();
            SpecialAcceptancerow[0] = 5;
            SpecialAcceptancerow[1] = "SpecialAcceptance";
            dt.Rows.Add(SpecialAcceptancerow);

            return dt;

        }

        public static bool CreateStage(SupplyInfo supply, List<StorageTempInfo> details)
        {
            using (var context = new Entities())
            {
                var findsupply = context.supplies.FirstOrDefault(a => a.supply_id == supply.supply_id);
                if (findsupply == null)
                    return false;

                //this has created the storages data.
                findsupply.supply_hasStoraged = 1;

              //  int i=1;
                foreach (var detail in details)
                {
                    for (int i = 0; i < detail.stacker_number; i++)
                    {
                        var newdetail = new supply_detail();
                        newdetail.stacker_id = detail.stacker_id;
                        newdetail.supply_id = supply.supply_id;
                        newdetail.supply_detail_code = detail.stacker_barcode + i.ToString().PadLeft(5, '0');
                        newdetail.supply_detail_isvalid = true;
                        newdetail.supply_detail_number = supply.supply_kg;
                        newdetail.supply_detail_invalid_time = supply.supply_invalid_time;

                        context.supply_detail.AddObject(newdetail);
                    }
                }

                context.SaveChanges();
            }

            return true;
        }



        public static bool SetExpireStorageStatus(int code)
        {
            using (var context = new Entities())
            {
                var findstorage = context.supplies.FirstOrDefault(a => a.supply_id == code);
                if (findstorage != null)
                {
                    findstorage.supply_checkedstatus = (int)CheckStatus.Expired;
                    context.SaveChanges();
                }

                return false;
            }
        }

        public static bool DeactiveStorage(int supply_id)
        {
            using (var context = new Entities())
            {
                context.sp_deactiveStorage(supply_id);

                return true;
            }
        }

        public static bool DeactiveStorageOut(int out_id)
        {
            using (var context = new Entities())
            {
                context.sp_deactiveStorageOut(out_id);
                return true;
            }
        }



        public static int  GetSupplyDetails(int supply_id)
        {

            using (var context = new Entities())
            {
                var resource = context.supply_detail.
                    Where(a => a.supply_id == supply_id
                    && a.supply_isInProducing==true
                    );

                return resource.Count();
            }
        }

        public static bool CancelSupply(int supply){
            return true;
        }


    }
}
